package com.appkubes.fintech.restful.application.user.controller;

import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.core.po.FintechUserBaseInfo;
import com.appkubes.fintech.restful.entity.vo.BaseUserKeyVO;
import com.appkubes.fintech.restful.application.user.entity.dto.CreditUserBaseInfoDTO;
import com.appkubes.fintech.restful.application.user.entity.vo.UserBaseInfoVO;
import com.appkubes.fintech.restful.application.user.service.UserBaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@Api(tags = "用户基础信息")
@RestController
@RequestMapping("/user/baseInfo")
public class UserBaseInfoController {

    @Resource
    UserBaseInfoService baseInfoService;

    @ApiOperation("更新或添加用户基本信息")
    @PostMapping("/upsert")
    public Response upsert(@RequestBody @Valid UserBaseInfoVO userBaseInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        Integer effectRow = baseInfoService.baseInfoCertificate(userBaseInfoVO);
        return Response.success(effectRow);
    }

    @ApiOperation("查询用户基本信息")
    @GetMapping("/getBaseInfo")
    public Response<CreditUserBaseInfoDTO> getUserBaseInfo(@Valid BaseUserKeyVO baseUserKeyVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }

        FintechUserBaseInfo userBaseInfo = baseInfoService.getUserBaseInfoDao().selectByPrimaryKey(baseUserKeyVO.getUserKey());
        CreditUserBaseInfoDTO userBaseInfoDTO = new CreditUserBaseInfoDTO();
        if (userBaseInfo != null)
            BeanUtils.copyProperties(userBaseInfo, userBaseInfoDTO);
        return Response.success(userBaseInfoDTO);
    }

}
